package com.saicent.note.controller;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.saicent.note.core.Article;
import com.saicent.note.core.Category;
import com.saicent.note.service.ArticleService;
import com.saicent.note.service.JspFileArticleService;
import com.saicent.support.service.ServiceException;
import com.saicent.support.utils.ObjectUtils;
import com.saicent.support.utils.StringUtils;
import com.saicent.support.web.ws.rs.Response;

@Controller
@RequestMapping("articles")
public class ArticleController extends AbstractController implements InitializingBean {
	
	private ArticleService articleService;
	
	public ArticleService getArticleService() {
		return articleService;
	}

	@Autowired
	public void setArticleService(ArticleService articleService) {
		this.articleService = articleService;
	}

	@RequestMapping("{categoryId}/{articleId}")
	public String showArticle(
			@PathVariable("categoryId") String categoryId,
			@PathVariable("articleId") String articleId) {
		Article  article  = getArticleManager().getArticle(articleId);
		Category category = getArticleManager().getCategory(categoryId);
		category = (category == null && DEFAULT_CATEGORY.getId().equals(categoryId)) 
				? DEFAULT_CATEGORY : category;
		if (!ObjectUtils.isNoneNull(article, category)) {
			return ArticleController.class.getName();
		}
		if (!article.getCategories().contains(category.getName())
				&& category != DEFAULT_CATEGORY) {
			return ArticleController.class.getName();
		}
		currentCategory(category);
		return getLogicLocation(article);
	}
	
	@RequestMapping("delete")
	@ResponseBody
	public Response deleteArticle(@RequestParam("articleId") String articleId) {
		try {
			getArticleManager().deleteArticle(articleId);
			getArticleManager().reload();
			return Response.build(Response.RESPONSE_STATUS_OK);
		} catch (Exception e) {
			return Response.build(Response.RESPONSE_STATUS_ERROR).error(500, e.getMessage());
		}
	}
	
	@RequestMapping("create")
	public String createArticle() {
		return "article-create";
	}
	
	@RequestMapping("save")
	@ResponseBody
	public Response saveArticle(
			@RequestParam("title") String title,
			@RequestParam("content")String content,
			@RequestParam("categories") String categories) {
		try {
			Article article = Article.createArticle(title, categories, content);
			articleService.createArticle(article);
			getArticleManager().reload();
		} catch (ServiceException e) {
			return Response.build(Response.RESPONSE_STATUS_ERROR).error(500, e.getMessage());
		}
		return Response.build(Response.RESPONSE_STATUS_OK);
	}
	
	private String getLogicLocation(Article article) {
		String location = StringUtils.strip(article.getLocation(), JSP_BASE_LOCATION);
		return StringUtils.substringBefore(location, ".");
	}

	@Override
	public void afterPropertiesSet() throws Exception {
		if (articleService instanceof JspFileArticleService) {
			JspFileArticleService jspFileArticleService = (JspFileArticleService) articleService;
			jspFileArticleService.setJspFileDirectory(getServletContext().getRealPath("/WEB-INF/view/notes"));
		}
	}
	
}
